<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Ignore extends AUTH_Controller {

  /**
   * Process ignore request
   * 
   */
  public function index() {

    // Chekcing request type
    if ($this->is_post() && $this->is_ajax()) {

      // Init variables
      $total_ignore_count = 0;
      $ignore_post_status = 0;
      $response_array = array('status' => 0, 'msg' => '', 'data' => '');

      // Setting valid request params
      $ignore_types = array('tweet', 'reply', 'poll', 'comment', 'photo');

      // Getting request params
      $ignore_type = $this->sanitize($this->input->post('ignore_type', TRUE));
      $ignore_type_id = $this->sanitize($this->input->post('ignore_type_id', TRUE));

      // Checking request params
      if (!empty($ignore_type) && !empty($ignore_type_id) && is_numeric($ignore_type_id) && in_array($ignore_type, $ignore_types)) {

        // Adding ignore
        $add_ignore = $this->ignore_model->add_ignore($ignore_type, $ignore_type_id, $this->current_user['user_id']);

        // Getting ignores
        $get_ignore = $this->ignore_model->get_ignore($ignore_type, $ignore_type_id);
        $total_ignore_count = $get_ignore['count'];

        // Delete when 10th member ignore
        if ($total_ignore_count > 9) {
          $ignore_post_status = $this->ignore_post($ignore_type, $ignore_type_id);
        }

        $response_array['status'] = 1;
        $response_array['data'] = array('report_status' => $add_ignore['status'], 'ignore_status' => $ignore_post_status, 'total_ignore_count' => $total_ignore_count);
      }

      // Responding json
      die(json_encode($response_array));
    }

    redirect();
    return;
  }

  /**
   * To process a post deleting
   * 
   */
  private function ignore_post($ignore_type = NULL, $ignore_type_id = NULL) {

    // init variables
    $post_delete_process = array('status' => 0, 'result' => '', 'count' => 0);

    // Checking ignore details
    if (!empty($ignore_type) && !empty($ignore_type_id) && is_numeric($ignore_type_id)) {

      // Processing delete operation
      switch ($ignore_type) {

        case 'tweet':
          $post_delete_process = $this->tweet_model->delete_tweet($ignore_type_id);
          break;

        case 'reply':
          $post_delete_process = $this->tweet_model->delete_reply($ignore_type_id);
          break;

        case 'poll':
          $post_delete_process = array('status' => 0, 'result' => '', 'count' => 0);
          break;

        case 'photo':
          $post_delete_process = array('status' => 0, 'result' => '', 'count' => 0);
          break;

        case 'comment':
          $post_delete_process = $this->comment_model->del_comment_by_id($ignore_type_id);
          break;

        default :
          $post_delete_process = array('status' => 0, 'result' => '', 'count' => 0);
          break;
      }
    }

    // Returning result status
    return $post_delete_process['status'];
  }

}

?>